package com.bw.day03.utlis

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.bw.base.net.entity.Res
import java.util.concurrent.atomic.AtomicBoolean
//数据倒灌
class SingleLiveData<T> : MutableLiveData<T>() {
    var flag=AtomicBoolean(false)//是否发生了变化（具有原子性 多线程也会确保数据的单一性）

    override fun postValue(value: T) {
        flag.set(true)//数据发生了变化
        super.postValue(value)
    }
    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        super.observe(owner){
            if (flag.compareAndSet(true,false)){
                observer.onChanged(it)
            }
        }
    }
}